package com.conferenceroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.conferenceroom.entity.User;
import com.conferenceroom.mapper.UserMapper;
import com.conferenceroom.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<User> getUsersByType(Integer userType) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_type", userType);
        return baseMapper.selectList(wrapper);
    }

    // UserServiceImpl.java
    @Override
    public IPage<User> pageByCondition(IPage<User> page, User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (user.getUserId() != null) {
            wrapper.eq("user_id", user.getUserId());
        }
        if (StringUtils.hasLength(user.getUsername())) {
            wrapper.like("username", user.getUsername());
        }
        if (StringUtils.hasLength(user.getRealName())) {
            wrapper.like("real_name", user.getRealName());
        }
        if (StringUtils.hasLength(user.getUsername())) {
            wrapper.like("realName", user.getRealName());
        }
        if (user.getUserType() != null) {
            wrapper.eq("user_type", user.getUserType());
        }
        if (user.getStatus() != null) {
            wrapper.eq("status", user.getStatus());
        }

        return baseMapper.selectPage(page, wrapper);
    }



    @Override
    public List<Map<String, Object>> getUserIdsByUsername(String username) {
        return baseMapper.selectUserIdsByUsername(username);
    }


}
